/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cs342.project1.mathNotebook.data;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class EncryptionDecryptionHandler {

    public static void main(String[] args) {
        try{
            EncryptionDecryptionHandler en = new EncryptionDecryptionHandler();
            SecretKey desKey;
            // Create a key.
            byte[] desKeyData = "adminadmin".getBytes();
            DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
            SecretKeyFactory deskeyFactory = SecretKeyFactory.getInstance("DES");
            desKey = deskeyFactory.generateSecret(desKeySpec);

        String filename = "/students/hawrar91/softengprojtwo/compe.xml";

        Encryption(filename,desKey);
        Decryption(filename.substring(0,filename.length()-3)+".dat",desKey);
        }catch(Exception e)
        {
            System.out.println("error "+e);
        }
    }


    public static void Encryption(String filename, SecretKey desKey) {
        try {
            //System.out.println("name in encryption "+filename);
            FileInputStream in = new FileInputStream(filename);
            //remove .gz from currentFilename
            String temp = filename.substring(0,filename.length()-3);
            temp = temp+".des";
            FileOutputStream fout = new FileOutputStream(temp);

            // Use Data Encryption Standard cipher
            Cipher des = Cipher.getInstance("DES");
            des.init(Cipher.ENCRYPT_MODE, desKey);

            byte[] input = new byte[64];
            while (true) {
                int bytesRead = in.read(input);
                if (bytesRead == -1) break;
                byte[] output = des.update(input, 0, bytesRead);
                if (output != null) fout.write(output);
            }

            byte[] output = des.doFinal();
            if (output != null) fout.write(output);
            in.close();
            fout.flush();
            fout.close();

            File deletxmlf = new File(filename);
            deletxmlf.delete();
        }catch (InvalidKeyException e) {System.err.println(e);}
        catch (NoSuchAlgorithmException e) {
            System.err.println(e);
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {System.err.println(e);}
        catch (BadPaddingException e) {System.err.println(e);}
        catch (IllegalBlockSizeException e) {System.err.println(e);}
        catch (IOException e) {System.err.println(e);}
    }

    public static void Decryption(String filename, SecretKey desKey) {
        try {
            //System.out.println("name in decryption "+filename);
            FileInputStream fin = new FileInputStream(filename);
            FileOutputStream out = new FileOutputStream(filename.substring(0,filename.length()-4)+".gz");
            // Use Data Encryption Standard cipher
            Cipher des = Cipher.getInstance("des");
            des.init(Cipher.DECRYPT_MODE, desKey);

            byte[] input = new byte[64];
            while (true) {
                int bytesRead = fin.read(input);
                if (bytesRead == -1) break;
                byte[] output = des.update(input, 0, bytesRead);
                if (output != null) out.write(output) ;
            }

            byte[] output = des.doFinal();
            if (output != null) out.write(output);
            fin.close();
            out.close();
        }catch (InvalidKeyException e) {System.err.println(e);}
        catch (NoSuchAlgorithmException e) {
            System.err.println(e);
            e.printStackTrace();
        }
        catch (NoSuchPaddingException e) {System.err.println(e);}
        catch (BadPaddingException e) {System.err.println(e);}
        catch (IllegalBlockSizeException e) {System.err.println(e);}
        catch (IOException e) {System.err.println(e);}
    }
}
